#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <ApplicationServices/ApplicationServices.h>
#include <QuickLook/QuickLook.h>
#include "OpenDDSFile.h"
#include "DDSDrawToImage.h"

/* -----------------------------------------------------------------------------
   Generate a preview for file

   This function's job is to create preview for designated file
   ----------------------------------------------------------------------------- */

OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
    DDSFile *file = DDSOpenFile(url);
	if (!file) return -1;
	
	CFIndex fileWidth = DDSGetWidth(file);
	CFIndex fileHeight = DDSGetHeight(file);
	
	CGImageRef result = DDSCreateImage(file, fileWidth, fileHeight);
	if (!result)
	{
		DDSDestroy(file);
		return -1;
	}
	
	CGContextRef context = QLPreviewRequestCreateContext(preview, CGSizeMake((CGFloat) fileWidth, (CGFloat) fileHeight), true, NULL);
	CGContextDrawImage(context, CGRectMake(0.0, 0.0, (CGFloat) fileWidth, (CGFloat) fileHeight), result);
	QLPreviewRequestFlushContext(preview, context);
	CGContextRelease(context);
	
	DDSDestroy(file);
    return noErr;
}

void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
{
    // implement only if supported
}
